//
//  ByShopDetailProductInfoCell.m
//  hutouben_ios
//
//  Created by pro on 16/11/29.
//  Copyright © 2016年 mc. All rights reserved.
//

#import "ByCellProductDetailInfo.h"

#pragma mark - layout
@implementation ByCellLayoutProductDetailInfo

- (void)setModel:(ByModelProductDetail *)model {
    
    _model = model;
    
    CGFloat x, y, width, height;
    NSString *text;
    // 分享
    width           = 60;
    height          = 60;
    x               = SCREEN_WIDTH - width;
    y               = 0;
    _shareFrame     = CGRectMake(x, y, width, height);
    
    // 标题
    x               = 10;
    y               = 10;
    width           = CGRectGetMinX(_shareFrame) - 2 * x;
    height          = [model.title getRectOfStringWithSize:CGSizeMake(width, 1000) font:15].size.height;
    height          = MAX(height, 20);
    _titleFrame     = CGRectMake(x, y, width, height);
    
    // 价格
    text            = [NSString stringWithFormat:@"￥%.1lf", [model.price floatValue]];
    x               = 10;
    y               = CGRectGetMaxY(_titleFrame) + 10;
    width           = [text getSizeOfString:18].width + 20;
    height          = 20;
    _priceFrame     = CGRectMake(x, y, width, height);

    // 原价
    text            = [NSString stringWithFormat:@"价格：￥%.1lf", [model.oriPrice floatValue]];
    x               = 10;
    y               = CGRectGetMaxY(_priceFrame) + 10;
    width           = [text getSizeOfString:14].width + 20;
    height          = 20;
    _oriPriceFrame  = CGRectMake(x, y, width, height);
    
    // 快递
    x               = 10;
    y               = CGRectGetMaxY(_oriPriceFrame) + 10;
    width           = (SCREEN_WIDTH - 2 * x) / 3;
    height          = 20;
    _postageFrame   = CGRectMake(x, y, width, height);
    
    // 销量
    x               = CGRectGetMaxX(_postageFrame);
    y               = CGRectGetMinY(_postageFrame);
    width           = CGRectGetWidth(_postageFrame);
    height          = CGRectGetHeight(_postageFrame);
    _salesFrame     = CGRectMake(x, y, width, height);

    // 产地
    x               = CGRectGetMaxX(_salesFrame);
    y               = CGRectGetMinY(_salesFrame);
    width           = CGRectGetWidth(_salesFrame);
    height          = CGRectGetHeight(_salesFrame);
    _areaFrame      = CGRectMake(x, y, width, height);

    // cell
    x               = 0;
    y               = 0;
    width           = SCREEN_WIDTH;
    height          = CGRectGetMaxY(_areaFrame) + 10;
    _cellFrame      = CGRectMake(x, y, width, height);
}

@end



#pragma mark - cell
@implementation ByCellProductDetailInfo
{
    UILabel     *_titleLabel;       // 标题
    UIButton    *_sharebtn;         // 分享

    UILabel     *_priceLabel;       // 价格
    UILabel     *_oriPriceLabel;    // 原价

    UILabel     *_postageLabel;     // 邮费
    UILabel     *_salesLabel;       // 销量
    UILabel     *_areaLabel;        // 产地

}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.backgroundColor = [UIColor whiteColor];
        [self setupUI];
    }
    return self;
}
- (void)setupUI {
 
    // 标题
    _titleLabel = [[UILabel alloc] init];
    _titleLabel.adjustsFontSizeToFitWidth = YES;
    _titleLabel.numberOfLines = 0;
    _titleLabel.font = FONT(15);
    _titleLabel.textColor = TEXT_COLOR;
    _titleLabel.textAlignment = NSTextAlignmentLeft;
    [self.contentView addSubview:_titleLabel];
    
    // 分享
    _sharebtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _sharebtn.titleLabel.font = FONT(12);
    [_sharebtn setTitle:@"分享" forState:UIControlStateNormal];
    [_sharebtn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    [_sharebtn setImage:[UIImage imageNamed:@"product_detail_share"] forState:UIControlStateNormal];
    [self.contentView addSubview:_sharebtn];
    [_sharebtn addTarget:self action:@selector(shareAction) forControlEvents:UIControlEventTouchUpInside];
    
    // 价格
    _priceLabel = [[UILabel alloc] init];
    _priceLabel.adjustsFontSizeToFitWidth = YES;
    _priceLabel.font = FONT(18);
    _priceLabel.textColor = [UIColor orangeColor];
    _priceLabel.textAlignment = NSTextAlignmentLeft;
    [self.contentView addSubview:_priceLabel];
    
    // 原价
    _oriPriceLabel = [[UILabel alloc] init];
    _oriPriceLabel.adjustsFontSizeToFitWidth = YES;
    _oriPriceLabel.font = FONT(14);
    _oriPriceLabel.textColor = [UIColor lightGrayColor];
    _oriPriceLabel.textAlignment = NSTextAlignmentLeft;
    [self.contentView addSubview:_oriPriceLabel];
    
    // 邮费
    _postageLabel = [[UILabel alloc] init];
    _postageLabel.adjustsFontSizeToFitWidth = YES;
    _postageLabel.font = FONT(14);
    _postageLabel.textColor = [UIColor lightGrayColor];
    _postageLabel.textAlignment = NSTextAlignmentLeft;
    [self.contentView addSubview:_postageLabel];
    
    // 销量
    _salesLabel = [[UILabel alloc] init];
    _salesLabel.adjustsFontSizeToFitWidth = YES;
    _salesLabel.font = FONT(14);
    _salesLabel.textColor = [UIColor lightGrayColor];
    _salesLabel.textAlignment = NSTextAlignmentCenter;
    [self.contentView addSubview:_salesLabel];
    
    // 产地
    _areaLabel = [[UILabel alloc] init];
    _areaLabel.adjustsFontSizeToFitWidth = YES;
    _areaLabel.font = FONT(14);
    _areaLabel.textColor = [UIColor lightGrayColor];
    _areaLabel.textAlignment = NSTextAlignmentRight;
    [self.contentView addSubview:_areaLabel];

}

- (void)setLayout:(ByCellLayoutProductDetailInfo *)layout {
    
    _layout = layout;
    
    [self setNeedsLayout];
}

- (void)layoutSubviews {
    
    [super layoutSubviews];
    
    ByModelProductDetail *model = _layout.model;
    
    // 设置布局
    _sharebtn.frame                 = _layout.shareFrame;
    _sharebtn.imageEdgeInsets       = UIEdgeInsetsMake(-20, 20, 0, 0);
    _sharebtn.titleEdgeInsets       = UIEdgeInsetsMake(0, 0, -20, 10);
    _titleLabel.frame               = _layout.titleFrame;
    _priceLabel.frame               = _layout.priceFrame;
    _oriPriceLabel.frame            = _layout.oriPriceFrame;
    _postageLabel.frame             = _layout.postageFrame;
    _salesLabel.frame               = _layout.salesFrame;
    _areaLabel.frame                = _layout.areaFrame;

    // 设置属性
    _titleLabel.text                = model.title;
    _priceLabel.text                = [NSString stringWithFormat:@"￥%.1lf", [model.price floatValue]];
    NSMutableAttributedString *attr = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"价格：￥%.1lf", [model.oriPrice floatValue]]];
    [attr addAttribute:NSStrikethroughStyleAttributeName
                    value:@(NSUnderlinePatternSolid | NSUnderlineStyleSingle)
                    range:NSMakeRange(3, attr.length - 3)];
    _oriPriceLabel.attributedText   = attr;
    _postageLabel.text              = [NSString stringWithFormat:@"快递：%@", @"0.00"];
    _salesLabel.text                = [NSString stringWithFormat:@"月销量%@笔", model.total_sales];
    _areaLabel.text                 = [NSString stringWithFormat:@"%@", model.place_origin];

}

#pragma mark - 点击事件
- (void)shareAction {
    
    if (self.shareBlock) {
        self.shareBlock();
    }
}

@end
